# -*- coding:utf-8 -*-
# !/usr/bin/env
# @description LaGou采集脚本公共方法文件
# @author:jack.spanrrows
# @datetime: 2018/08/04 12:20
# @copyRight jack.spanrrows@gmail.com

# 获取目录是否存在，不存在则创建
import os
import time
import pycurl
from io import *
from scrapy_web_py3.config.testing import constants


def check_dir_exists(dir):
    if os.path.exists(dir) is False:
        os.makedirs(dir)
    return True

# 获取格式化日期
def get_date(timestamp=False):
    if timestamp is False:
        timestamp = get_init_time()
    time_local = time.localtime(timestamp)
    return time.strftime("%Y-%m-%d %H:%M:%S",time_local)

# 获取当前时间戳
def get_init_time():
    totime = time.time()
    totime = int(totime)
    return totime

def get_micro_time():
    totime = time.time() * 1000
    totime = int(totime)
    return totime

def get_schedule(a, b, c):
    '''
        a:已经下载的数据块
        b:数据块的大小
        c:远程文件的大小
    '''
    per = 100.0 * a * b / c
    if per > 100:
        per = 100
        print("100%下载完成")
        exit()
    print('正在下载%.2f%%' % per)

def set_match_number(match_number):
    """
    根据抓取数据格式化需要数据
    Args:
        match_number:
    Returns:
    """
    if match_number is not False or match_number is not None:
        if match_number.find("星期一")>=0:
            match_number = match_number.replace(' ','')
            match_number = match_number.replace('星期一','周一')
        if match_number.find("星期二")>=0:
            match_number = match_number.replace(' ','')
            match_number = match_number.replace('星期二','周二')
        if match_number.find("星期三")>=0:
            match_number = match_number.replace(' ','')
            match_number = match_number.replace('星期三','周三')
        if match_number.find("星期四")>=0:
            match_number = match_number.replace(' ','')
            match_number = match_number.replace('星期四','周四')
        if match_number.find("星期五")>=0:
            match_number = match_number.replace(' ','')
            match_number = match_number.replace('星期五','周五')
        if match_number.find("星期六")>=0:
            match_number = match_number.replace(' ','')
            match_number = match_number.replace('星期六','周六')
        if match_number.find("星期日")>=0:
            match_number = match_number.replace(' ','')
            match_number = match_number.replace('星期日','周日')
        return match_number


def var_dump(message, logs_files=constants.run_logs, debug_open = True, type ="INFO", write_type ='all', strs =',', is_print = True):
    check_dir_exists(constants.run_logs_dir)
    if isinstance(message, int):
        message = str(message)
    info = "[%s(%s) -- %s -- %s]" % (get_date(), get_micro_time(), type, message)
    if is_print is True:
        print(info)
    if debug_open is True and write_type is "all":
        with open(logs_files, 'a') as fp:
            fp.write(info)
            fp.write("\n")
    if write_type is "any":
        with open(logs_files, 'a') as fp:
            fp.write(message+"%s" % (strs))
            fp.write("\n")


def curl_post(url, encoding='gb2312', accept = True):
    cu = pycurl.Curl()
    cu.setopt(pycurl.CONNECTTIMEOUT, 30)
    buffer = BytesIO()
    cu.setopt(cu.WRITEDATA, buffer)
    if accept is True:
        cu.setopt(pycurl.ACCEPT_ENCODING,"gzip, deflate")
    cu.setopt(pycurl.FOLLOWLOCATION, 1)
    cu.setopt(pycurl.URL, url)
    cu.setopt(pycurl.USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36")
    cu.setopt(pycurl.HTTPHEADER, ["Expect:"])
    cu.perform()
    # if encoding is True:
    html = buffer.getvalue().decode(encoding, "ignore")
    # else:
    #     html = buffer.getvalue()
    cu.close()
    return html


def filter_data(data, left_chars = '(', right_chars = ')'):
    """
    网页json数据过滤
    :return:
    """
    if data.find(left_chars) >=0:
        data = data.split(left_chars)
        if len(data) >= 2:
            data = data[1].split(right_chars)
            if len(data) >= 2:
                data = '%s}' % data[0]
    return data

def filter_sporttery_json(data, left_char = '(', right_char = ');', replace_chars = None):
    if isinstance(data,str) is not True:
        return False
    data = data.replace("getData(", "")
    data = data.replace("hb_odds(", "")
    data = data.replace("asia_tb(", "")
    data = data.replace("footb_info(", "")
    data = data.replace("getMatchList(", "")
    if data.find("getResultHistoryInfo(") >= 0:
        print("当前替换getResultHistoryInfo")
        data = data.replace("getResultHistoryInfo(", "")
    if data.find("getAwayInfo(") >= 0:
        print("当前替换getAwayInfo(")
        data = data.replace("getAwayInfo(", "")
    if data.find("getFutureMatchesInfo(") >= 0:
        print("当前替换getFutureMatchesInfo(")
        data = data.replace("getFutureMatchesInfo(", "")
    if data.find("match_detail") >= 0:
        data = data.replace(" ","")
        data = data.replace("varmatch_detail=","")
        data = data[:-1]
    if data.find("match_updated") >= 0:
        data = data.replace(" ","")
        data = data.replace("varmatch_updated=","")
        data = data[:-1]
    if data.find("getMatchLiveBack(") >= 0:
        print("当前替换getMatchLiveBack(")
        data = data.replace("getMatchLiveBack(", "")
    if data.find("tabCcup")>=0:
        print("当前替换tabCcup(")
        data = data.replace("tabCcup(", "")
    if data.find("footb_group(") >= 0:
        print("当前替换footb_group(")
        data = data.replace("footb_group(", "")
    if replace_chars is not None:
        data = data.replace(replace_chars, "")
    data = data.replace(");", "")
    # if data.find(right_char) >= 0:
    #     data = data.split(right_char, 1)
    #     if len(data) >= 2:
    #         data = data[1].split(left_char, 1)
    #         if len(data) >= 2:
    #             data = data[0]
    return data

from sports_spiders.librarys import MongoCache as mongo
def get_mongo_cache_data(val, key = 'club_name', fields_key = "club_abbr_name"):
    fields_dict = {fields_key:1}
    get_full_team = mongo.MongoCache().find_one(key = key, val = val, field_dict = fields_dict)
    if get_full_team is None or get_full_team is False:
        # key = "club_abbr_name"
        get_full_team = mongo.MongoCache().find_one(key = key, val = val, field_dict = fields_dict)
        if get_full_team is False or get_full_team is None:
            club_abbr_name = val
        else:
            club_abbr_name = get_full_team[fields_key]
    else:
        if fields_key not in get_full_team.keys():
            return val
        else:
            club_abbr_name = get_full_team[fields_key]
    return club_abbr_name
